package com.wwwarehouse.usercenter.fragment.permission_recycle;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;

import com.wwwarehouse.usercenter.R;
import com.wwwarehouse.usercenter.adapter.AuthorityDistributeRvAdapter;
import com.wwwarehouse.usercenter.bean.permission_recycle.AuthorizedOutItensBean;

import java.util.List;

/**
 * Created by xiaoke.ni on 2017/10/13.
 */

public class PRUserCardAuthorityAdapter extends RecyclerView.Adapter<PRUserCardAuthorityAdapter.PRAuthorityAdapterHolder> {

    private final int mRvHeight;
    private List<AuthorizedOutItensBean.UserCardAuthoritiesBean> mDataList;
    private Context mContext;

    public interface OnItemClickListener {
        void onButtonClicked(int position);

    }

    private PRUserCardAuthorityAdapter.OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(PRUserCardAuthorityAdapter.OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }

    public PRUserCardAuthorityAdapter(List<AuthorizedOutItensBean.UserCardAuthoritiesBean> dataList, int height) {
        this.mDataList = dataList;
        mRvHeight = height;
    }

    @Override
    public PRUserCardAuthorityAdapter.PRAuthorityAdapterHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        mContext = parent.getContext();
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_pr_user_card, parent, false);
        return new PRUserCardAuthorityAdapter.PRAuthorityAdapterHolder(view);
    }

    @Override
    public void onBindViewHolder(final PRUserCardAuthorityAdapter.PRAuthorityAdapterHolder holder, final int position) {
        ViewGroup.LayoutParams params = holder.itemView.getLayoutParams();
        params.height = (int) (mRvHeight * 0.85 / 9);
        holder.itemView.setLayoutParams(params);
        AuthorizedOutItensBean.UserCardAuthoritiesBean userCardAuthoritiesBean = mDataList.get(position);
        holder.mBtn.setText(userCardAuthoritiesBean.getCardName());
        if (mOnItemClickListener != null) {
            holder.mBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnItemClickListener.onButtonClicked(position);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return mDataList == null ? 0 : mDataList.size();
    }

    static class PRAuthorityAdapterHolder extends RecyclerView.ViewHolder {

        public Button mBtn;

        public PRAuthorityAdapterHolder(View itemView) {
            super(itemView);
            mBtn = (Button) itemView.findViewById(R.id.btn);
        }
    }
}
